package com.chinadaas.hangzhou.archetypeweb.controller;

import com.chinadaas.hangzhou.archetypeweb.domain.po.AlertRulePO;
import com.chinadaas.hangzhou.archetypeweb.domain.vo.ResultVO;
import com.chinadaas.hangzhou.archetypeweb.mapper.AlertRuleDao;
import com.chinadaas.hangzhou.archetypeweb.util.ElasticDao;
import com.chinadaas.hangzhou.archetypeweb.util.EtcdUtil;
import com.chinadaas.hangzhou.archetypeweb.util.RedisUtil;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * @author: XiaoDong.Xie
 * @create: 2021-11-03 16:12
 * @description:
 */
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private AlertRuleDao alertRuleDao;

    @Autowired
    private EtcdUtil etcdUtil;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private ElasticDao elasticDao;

    @GetMapping("/testMysql")
    public ResultVO testMysql() {
        List<AlertRulePO> alertRulePOS = alertRuleDao.selectList(null);
        return new ResultVO("200", alertRulePOS);
    }

    @GetMapping("/testEtcd")
    public ResultVO testEtcd() throws ExecutionException, InterruptedException {
        etcdUtil.putEtcdInfo("test_etcd", "value");
        System.out.println(etcdUtil.getEtcdInfo("test_etcd"));
        etcdUtil.delEtcdInfo("test_etcd");
        System.out.println("ok");
        return new ResultVO("2000");
    }

    @GetMapping("/testRedis")
    public ResultVO testRedis() {
        redisUtil.set("test_k1", "test_v1");
        System.out.println(redisUtil.get("test_k1"));
        redisUtil.del("test_k1");
        return new ResultVO("2000");
    }

    @GetMapping("/testES")
    public ResultVO testES() {
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()
                .filter(QueryBuilders
                        .rangeQuery("startTime")
                        .gte(1626087635803L)
                        .lte(1626087636240L));

        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder()
                .query(boolQueryBuilder)
                .size(10);

        SearchRequest searchRequest = new SearchRequest("platform_service_log");
        searchRequest.source(sourceBuilder);
        SearchResponse searchResponse = elasticDao.query(searchRequest, "platform_service_log");
        System.out.println(searchResponse);
        return null;
    }


    @GetMapping("/testKafka")
    public ResultVO testKafka() {
        return null;
    }
}
